<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';


class dao_cinema {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT ms.name AS city_name, m.* FROM tbl_cinema m LEFT JOIN tbl_city ms ON m.city_id = ms.id";
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function get_all_by_status_id($status_id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT ms.name AS status_name, m.* "
                . "FROM tbl_cinema m LEFT JOIN tbl_cinema_status ms ON m.status_id = ms.id "
                . "WHERE ms.id = " . $status_id;
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function save($id, $name, $address, $image, $city_id, $latitude, $longitude) {
        $db = new connection();
        $con = $db->open_connect();

        if ($id == 0) {
            $query = "INSERT INTO tbl_cinema(name, address, image, city_id, latitude, longitude) VALUES (
                '" . $name . "','" . $address . "','" . $image . "'," . $city_id . "," . $latitude . "," . $longitude . ")";
        } else {
            $query = "UPDATE tbl_cinema SET "
                    . "name = '" . $name . "',"
                    . "address = '" . $address . "',"
                    . "image = '" . $image . "',"
                    . "latitude = '" . $latitude . "',"
                    . "longitude = '" . $longitude . "',"
                    . "city_id = " . $city_id . " "
                    . "WHERE id = " . $id;
        }
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_by_id($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT ms.name AS city_name, m.* FROM tbl_cinema m LEFT JOIN tbl_city ms ON m.city_id = ms.id WHERE m.id = " . $id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function get_first() {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_cinema LIMIT 0, 1";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function delete($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_cinema WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_id_by_name($name) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT id FROM tbl_cinema WHERE UPPER(name) = UPPER('" . $name ."') LIMIT 0, 1";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }
}
